﻿/* this is just an XML reader thing
 * <?xml version="1.0" encoding="utf-8" ?> 
 * <appinfo> <version>1.5.0.0</version> 
 * <url>http://www.fiercemodder.com/Fierce%20Modder/v1.5.rar</url> 
 * <about>Remapped UI. Get License file from me</about>
 * </appinfo>
 * http://www.se7ensins.com/forums/threads/c-tut-check-for-update-button.373563/#post-2713258
 * */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;


namespace PvZTool
{
    class Class1
    {
        private void updButton_Click_1(object sender, EventArgs e)
        {
            string downloadUrl = "";
            Version newVersion = null;
            string aboutUpdate = "";
            string xmlUrl = "http://www.fiercemodder.com/Fierce%20Modder/update.xml";
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(xmlUrl);
                reader.MoveToContent();
                string elementName = "";
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "appinfo"))
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            elementName = reader.Name;
                        }
                        else
                        {
                            if
                              ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue)) switch (elementName)
                                {
                                    case "version": newVersion = new Version(reader.Value);
                                        break;
                                    case "url": downloadUrl = reader.Value;
                                        break;
                                    case "about": aboutUpdate = reader.Value;
                                        break;
                                }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Environment.Exit(1);
            }
            finally
            {
                if (reader != null)
                    reader.Close();

            }
            Version applicationVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            if (applicationVersion.CompareTo(newVersion) < 0)
            {
                string str = String.Format("New version found!\nYour version: {0}.\nNewest version: {1}. \nAdded in this version: {2}. ", applicationVersion, newVersion, aboutUpdate);
                if (DialogResult.No != MessageBox.Show(str + "\nWould you like to download this update?", "Check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    try
                    {
                     System.Diagnostics.Process.Start(downloadUrl);
                    }
                    catch { }
                    return;
                }
                else
                {
                    ;
                }
            }
            else
            {
                MessageBox.Show("Your version: " + applicationVersion + " is up to date.", "Check for Updates", MessageBoxButtons.OK, MessageBoxIcon.None);

            }
        }

    }
}
